package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.entity.Banner;
import com.jyy.prefabricated.entity.SysRole;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.BannerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class BannerService {

    @Autowired
    private BannerMapper bannerMapper;


    /**
     * 新增
     * @param banner
     * @return
     */
    public int insert(Banner banner){
        banner.setStatus(BaseStatusEnum.INVALID.getValue());
        bannerMapper.insert(banner);
        return banner.getId();
    }

    /**
     * 切换发布状态
     * @param id
     * @return
     */
    public int updateStatus(Integer id){
        Banner banner = bannerMapper.selectByPrimaryKey(id);
        if(banner.getStatus().equals(BaseStatusEnum.VALID.getValue())){
            banner.setStatus(BaseStatusEnum.INVALID.getValue());
        } else {
            banner.setStatus(BaseStatusEnum.VALID.getValue());
        }
        return bannerMapper.updateByPrimaryKeySelective(banner);
    }

    /**
     * 更新
     * @param banner
     * @return
     */
    public int update(Banner banner){
        if(banner.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        return bannerMapper.updateByPrimaryKeySelective(banner);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return bannerMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Banner selectById(Integer id){
        return bannerMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据参数查询
     * @param title 标题
     * @param status 状态
     * @return
     */
    public List<Banner> selectByParams(String title, Integer status){
        return bannerMapper.selectByParams(title, status);
    }

    /**
     * 批量切换发布状态
     * @param dto
     * @return
     */
    public int batchUpdateStatus(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return bannerMapper.batchUpdateStatus(dto.getIds(), dto.getStatus());
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    public int batchDelete(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return bannerMapper.batchDelete(dto.getIds());
    }
}
